import { Component, OnInit } from '@angular/core';
import { MARKET_SALESMAN_ROLE } from '../../../constant/dictionary';
import { MyModalHelper } from '@core/my-modal.helper';
import { NzMessageService } from 'ng-zorro-antd';
import { ActivatedRoute, Params } from '@angular/router';
import { TriggerStaticsService } from '@core/routes-services/trigger-statics.service';

@Component({
  selector: 'app-trigger-statics-list',
  templateUrl: './trigger-statics-list.component.html',
})
export class TriggerStaticsListComponent implements OnInit {

  isLoading = false;
  keyword: any;
  page: any = { number: 1, size: 10, totalElements: 0, content: [] };

  marketSalesmanRole: any[] = MARKET_SALESMAN_ROLE;

  constructor(private message: NzMessageService,
              private route: ActivatedRoute,
              private triggerStaticsService: TriggerStaticsService,
              private modalHelper: MyModalHelper) {
  }

  ngOnInit() {
    this.route.queryParams.forEach((params: Params) => {
      this.keyword = params['keyword'];
      this.initPage();
    });
  }


  initPage() {
    this.isLoading = true;
    this.triggerStaticsService.pagination(this.page, this.keyword).then((res) => {
      console.log(res);
      if (res.status.toString().startsWith('20')) {
        this.page = res.data;
        ++this.page.number;
      } else {
        this.message.warning(res.message);
      }
      this.isLoading = false;
    }, error => {
      this.message.error(error.message ? error.message : error.error);
      this.isLoading = false;
    });
  }

  /**
   * 搜索重置分页数据
   */
  search() {
    this.page = { number: 0, size: 10, totalElements: 0, content: [] };
    this.initPage();
  }

  changePageNo(pageNo) {
    this.page = { number: pageNo, size: 10, totalElements: 0, content: [] };
    this.initPage();
  }


}
